<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>券维护</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link href="/node_modules/LobsterUIFrame/layuiadmin/layui/css/layui.css" rel="stylesheet" />
    <link href="/node_modules/LobsterUIFrame/layuiadmin/style/admin.css" rel="stylesheet" />
    <link href="/node_modules/LobsterUIFrame/layuiadmin/layui/css/modules/layui-icon-extend/iconfont.css"
        rel="stylesheet" />
    <script src="/node_modules/LobsterUIFrame/layuiadmin/layui/layui.js"></script>
</head>

<body>
    <style>
        body,
        html {
            height: 100%;
            box-sizing: border-box;
            background: #fff;
        }
    </style>
    <div class="layui-fluid">
        <div class="layui-card layui-form">
            <div class="layui-form-item" style="padding-left:15px;">
                <label>券名</label>
                <div class="layui-inline" style="width:120px;">
                    <input type="text" class="layui-input" style="width:120px;" name="CouponName" autocomplete="off"
                        placeholder="请输入券名" />
                </div>
                <label>券类型</label>
                <div class="layui-inline" style="width:120px;">
                    <select name="CouponType">
                    </select>
                </div>
                <label>券状态</label>
                <div class="layui-inline" style="width:120px;">
                    <select name="CouponStatus">

                    </select>
                </div>
                <div class="layui-inline" style="width:120px;">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-list-submit"
                        id="btnSearch">
                        <i class="layui-icon layuiadmin-button-btn layui-icon-search"></i>查询
                    </button>
                </div>
            </div>
            <div class="layui-card-body">
                <table id="dataTable" lay-filter="dataTable"></table>
            </div>
        </div>
    </div>
</body>

</html>
<script type="text/html" id="edit">

    {{# if(d.CouponStatus==1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon  layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="publish"><i class="layui-icon layui-icon-ok"></i>发布</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-xs" lay-event="detail"><i class="layui-icon  layui-icon-edit"></i>查看详情</a> 
    {{# } }}
</script>
<script type="text/html" id="couponadd">
    <a class="layui-btn layui-btn-sm" lay-event="add" id="add"><i class="layui-icon layui-icon-add-1"></i>新增券</a>
</script>
<script>
    layui.config({
        base: '/node_modules/LobsterUIFrame/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    });

    layui.define(['index', 'layer', 'form', 'element', 'table', 'laydate'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var admin = layui.admin;
        var $ = layui.$;
        var tree = layui.tree;
        var form = layui.form;
        var oper = {
            data: {
                sellistinfo: {},//选中列表行 
                table: null,
                tabledata: [],
                listmodel: {},//列表行模型
                couponClass: [{ id: 1, text: '全品类' }],///券类别
                couponType: [{ id: 1, text: '满减券' }, { id: 2, text: '满折券' }, { id: 3, text: '立减券' }],//券类型
                couponStatus: [{ id: 1, text: '创建' }, { id: 2, text: '发布' }],//券状态
                validType: [{ id: 1, text: '固定有效期' }, { id: 2, text: '领取多少天有效' }],//有效期类型
                adaptiveRange: [{ id: 1, text: '全品类' }, { id: 2, text: '指定商品' }, { id: 3, text: '指定类目' },
                { id: 4, text: '指定品牌' }, { id: 5, text: '术后复诊' }, { id: 6, text: '康复复诊' }, { id: 7, text: '处方购药' },
                { id: 8, text: '计划购买' }, { id: 9, text: '会员购买' }],//适应范围 1 全品类 2指定商品 3指定类目 4指定品牌
                adaptivePeople: [{ id: 1, text: '全部会员' }, { id: 2, text: '付费会员' }],//适应人群 

            },
            initpage: function () {
                var self = this;
                self.initSelect(self.data.couponType, "CouponType");
                self.initSelect(self.data.couponStatus, "CouponStatus");
                form.render("select");
                self.tableLoad();
                self.initEvent();
            },
            initSelect: function (data, name) {
                var appendHtml = '';
                appendHtml = "<option value=0>全部</option>";
                data.forEach(a => {
                    appendHtml += "<option value=" + a.id + ">" + a.text + "</option>";
                })
                $("[name=" + name + "]").html(appendHtml);
            },
            getbaseurl: function (fun) {
                var url = "/lowcode/APP20210728143245471/member/?view=layui&fun=" + fun;
                return url;
            },
            //表数据加载
            tableLoad: function () {
                var self = this;
                var couponName = $("[name='CouponName']").val();
                var couponType = $("[name='CouponType']").val();
                var couponStatus = $("[name='CouponStatus']").val();
                self.data.classTable = table.render({
                    elem: "#dataTable",
                    where: {
                        couponName: couponName,
                        couponType: couponType,
                        couponStatus: couponStatus
                    },
                    toolbar: '#couponadd',
                    url: self.getbaseurl("coupon.getcouponlist"),
                    height: "full-100",
                    size: '10',
                    page: true, //开启分页 
                    loading: true,
                    cols: [[
                        { field: 'CouponName', title: '券名', width: '10%' },
                        {
                            field: 'CouponClass', title: '类别', width: '8%', templet: function (d) {
                                var filter = self.data.couponClass.filter(a => { return a.id == d.CouponClass; });
                                if (filter != null && filter.length > 0)
                                    return filter[0].text;
                            }
                        },
                        {
                            field: 'CouponType', title: '类型', width: '8%', templet: function (d) {
                                var filter = self.data.couponType.filter(a => { return a.id == d.CouponType; });
                                if (filter != null && filter.length > 0)
                                    return filter[0].text;
                            }
                        },
                        {
                            field: 'ValidType', title: '有效期类型', width: '15%', templet: function (d) {
                                var filter = self.data.validType.filter(a => { return a.id == d.ValidType; });
                                var returnstr = "";
                                if (filter != null && filter.length > 0)
                                    returnstr = filter[0].text
                                if (d.ValidType == 1)
                                    returnstr += "(" + (d.StartTime || "").substring(0, 10) + "-" + (d.EndTime || "").substring(0, 10) + ")";
                                else
                                    returnstr += "(" + d.ValidDays + "天)";
                                return returnstr;
                            }
                        },
                        { field: 'CouponAmount', title: '券额', width: '8%' },
                        { field: 'CouponDiscount', title: '券折扣', width: '8%' },
                        { field: 'FullImpairment', title: '满减值', width: '8%' },
                        { field: 'CouponCount', title: '券数量', width: '8%' },
                        { field: 'PerLimit', title: '每人限领张数', width: '8%' },
                        {
                            field: 'AdaptiveRange', title: '适应范围', width: '8%', templet: function (d) {
                                var filter = self.data.adaptiveRange.filter(a => { return a.id == d.AdaptiveRange; });
                                if (filter != null && filter.length > 0)
                                    return filter[0].text;
                            }
                        },
                        {
                            field: 'AdaptivePeople', title: '适应人群', width: '8%', templet: function (d) {
                                var filter = self.data.adaptivePeople.filter(a => { return a.id == d.AdaptivePeople; });
                                if (filter != null && filter.length > 0)
                                    return filter[0].text;
                            }
                        },
                        {
                            field: 'CouponStatus', title: '状态', width: '8%', templet: function (d) {
                                var filter = self.data.couponStatus.filter(a => { return a.id == d.CouponStatus; });
                                if (filter != null && filter.length > 0)
                                    return filter[0].text;
                            }
                        },
                        { field: 'operation', title: '操作', toolbar: '#edit', fixed: 'right', width: '10%' }
                    ]]
                    , done: function (res) {

                    }
                });
            },
            //打开模板编辑框
            openEdit(title, ishide) {
                var self = this;
                layer.open({
                    type: 2
                    , title: title
                    , content: "edit.html"
                    , area: ['50%', '100%']
                    , btn: ['保存', '取消']
                    , resize: false
                    , success: function (layero, index) {
                        if (ishide)
                            $(".layui-layer-btn0").hide();
                    }
                    , btn2: function (index, layero) {

                    }
                    , cancel: function () {

                    }
                    , yes: function (index, layero) {
                        var a = getResult()
                        if (a != null) {
                            self.data.listmodel = a;
                            layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                            self.save(() => {
                                self.tableLoad();
                                layer.close(index);
                            })
                        }
                    }
                });
            },
            //页面加载
            initEvent: function () {
                var self = this;
                //模板新增
                table.on("toolbar(dataTable)", function (obj) {
                    window.sellistinfo = null;
                    window.diclist = [{ name: "CouponClass", list: self.data.couponClass },
                    { name: "CouponType", list: self.data.couponType },
                    { name: "CouponStatus", list: self.data.couponStatus },
                    { name: "ValidType", list: self.data.validType },
                    { name: "AdaptiveRange", list: self.data.adaptiveRange },
                    { name: "AdaptivePeople", list: self.data.adaptivePeople },
                    ]
                    //新增
                    if (obj.event == "add") {
                        self.openEdit("新增券");
                    }
                });
                //模板编辑，删除
                table.on('tool(dataTable)', function (obj) {
                    var data = obj.data;//当前行数据

                    switch (obj.event) {
                        case 'edit':
                            window.sellistinfo = data;
                            window.diclist = [{ name: "CouponClass", list: self.data.couponClass },
                            { name: "CouponType", list: self.data.couponType },
                            { name: "CouponStatus", list: self.data.couponStatus },
                            { name: "ValidType", list: self.data.validType },
                            { name: "AdaptiveRange", list: self.data.adaptiveRange },
                            { name: "AdaptivePeople", list: self.data.adaptivePeople },
                            ]
                            self.openEdit(data.CouponName + '-编辑');
                            break;
                        case 'publish':
                            var tip = "确认发布吗?发布后不能修改"
                            layer.confirm(tip, { icon: 3, title: '提示' }, function (index) {
                                layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                                self.publish(data, () => {
                                    self.tableLoad();
                                    layer.close(index);
                                })
                            })
                            break;
                        case 'detail':
                            window.sellistinfo = data;
                            window.diclist = [{ name: "CouponClass", list: self.data.couponClass },
                            { name: "CouponType", list: self.data.couponType },
                            { name: "CouponStatus", list: self.data.couponStatus },
                            { name: "ValidType", list: self.data.validType },
                            { name: "AdaptiveRange", list: self.data.adaptiveRange },
                            { name: "AdaptivePeople", list: self.data.adaptivePeople },
                            ]
                            self.openEdit(data.CouponName + '-详情', true);
                            break;
                    }
                });
                //查询列表
                form.on("submit(LAY-list-submit)", function (data) {
                    self.tableLoad();
                });
            },
            save: function (succ) {
                var self = this;
                admin.req({
                    url: self.getbaseurl("coupon.savecoupon"),
                    method: "POST",
                    data: self.data.listmodel,
                    done: res => {
                        if (res.code == 0) {
                            layer.msg("保存成功!")
                            succ && succ();
                        }
                        else
                            layer.msg("保存失败!")
                    }
                });
            },
            /**
             * 发布
             * */
            publish: function (data, succ) {
                var self=this;
                admin.req({
                    url:self.getbaseurl("coupon.publishcoupon"),
                    method: "post",
                    data: {
                        CouponId: data.CouponId,
                        CouponStatus: 2
                    },
                    done: res => {
                        if (res.code == 0) {
                            layer.msg("保存成功!")
                            succ && succ();
                        }
                        else
                            layer.msg("保存失败!")
                    }
                });
            },
            //获取支付类型字典
            getDic: function () {
                return new Promise((resolve, reject) => {
                    admin.req({
                        url: '/orderpay/v1/PaymentType/GetDic',
                        method: "get",
                        data: {
                            code: "237"
                        },
                        done: res => {
                            resolve(res);
                        }
                    });
                })
            }
        }
        oper.initpage();
    });

</script>